var need_authorization_letter = ['120000','230000','310000','320000','340000','350000','420000','430000','500000','510000','520000','610000','630000','640000'];
var no_authorization_letter = ['110000','130000','140000','150000','210000','220000','330000','360000','370000','410000','440000','450000','460000','530000','540000','620000','650000'];

$(function() {
    $("#base_info_type li").on('click', function(event) {
        data = $(this).attr("data");
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        if(data == 1){
            $(".data-1").show();
            $(".data-2").hide();
            $(".data-3").hide();
        }else{
            $(".data-1").hide();
            $(".data-2").show();
        }
    });

    $("#principal_info_ul li").on('click', function(event) {
        data = $(this).attr("data");
        if(data == 0){
            if(is_principal_info.includes($("#base_info_province").val())){
                mviewToastr("error","该地区主体负责人必须是法人");
                return;
            }
            if(need_principal_info.includes($("#base_info_province").val())){
                $("#shouquan").show();
                $("#isshouquan").val(2);
            }else{
                $("#shouquan").hide();
                $("#isshouquan").val(1);
            }
            $(".data-3").show();
        }else{
            $("#shouquan").hide();
            $("#isshouquan").val(1);
            $(".data-3").hide();
        }
        $('input[name="is_principal_info"]').val(data);
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
    });

    $("#is_multiplex li").on('click', function(event) {
        data = $(this).attr("data");
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        console.log(data);
        if(data == 0){
            $(".data-4").show();
        }else{
            $(".data-4").hide();
        }
    });
    $('#base_info_type li[data="2"]').trigger("click");

    $("#miniapp_list li").on('click', function(event) {
        var appid = $(this).attr("data");
        const appindex = miniappLists.indexOf(appid);
        if (appindex !== -1) {
            miniappLists.splice(appindex,1);
            $(this).removeClass("active");
        } else {
            miniappLists.push(appid);
            $(this).addClass("active");
        }
    });

    var legal_person = $('#corp_id option[selected]').attr('data');
    var admin_person = $('#admin_id option[selected]').attr('data');
    var province = $('#corp_id option[selected]').attr('district');
    if((legal_person != "" && admin_person !="" && legal_person==admin_person) || no_authorization_letter.includes(province)){
        $("#admin_shouquan").val(1);
    }else{
        $("#admin_shouquan").val(2);
    }

    $("#corp_id").change(function(){
        province=$(this).children('option:selected').attr('district');
        legal_person = $(this).children('option:selected').attr('data');
        if(no_authorization_letter.includes(province) || (legal_person != "" && admin_person !="" && legal_person==admin_person)){
            $("#admin_shouquan").val(1);
        }else{
            $("#admin_shouquan").val(2);
        }
        if($("#admin_shouquan").val() == 2){
            $("#shouquan-show").show();
        }else{
            $("#shouquan-show").hide();
        }
    })
    $("#admin_id").change(function(){
        admin_person =$(this).children('option:selected').attr('data');
        if(no_authorization_letter.includes(province) || (legal_person != "" && admin_person !="" && legal_person==admin_person)){
            $("#admin_shouquan").val(1);
        }else{
            $("#admin_shouquan").val(2);
        }
        if($("#admin_shouquan").val() == 2){
            $("#shouquan-show").show();
        }else{
            $("#shouquan-show").hide();
        }
    })
});


function getComment() {
    var name = getRandomName(2);
    $("#comment").val("提供给我们商家线下开展活动扫码查询使用；承诺遵守中华人民共和国法律法规。")
}

// 获取指定范围内的随机数
function randomAccess(min, max) {
    return Math.floor(Math.random() * (min - max) + max)
}

// 解码
function decodeUnicode(str) {
    //Unicode显示方式是\u4e00
    str = "\\u" + str;
    str = str.replace(/\\/g, "%");
    //转换中文
    str = unescape(str);
    //将其他受影响的转换回原来
    str = str.replace(/%/g, "\\");
    return str;
}

function getRandomName(len) {
    let name = ""
    for (let i = 0; i < len; i++) {
        let unicodeNum = ""
        unicodeNum = randomAccess(0x4e00, 0x9fa5).toString(16)
        name += decodeUnicode(unicodeNum)
    }
    return name;
}

function submitBeian() {
    var corp_id = $("#corp_id").val();
    var admin_id = $("#admin_id").val();
    var appid = $("#appid").val();
    var comment = $("#comment").val();
    if (corp_id == "") {
        mviewToastr("error","请选择企业模版!");
        return;
    }
    if (admin_id == "") {
        mviewToastr("error","请选择管理员模版!");
        return;
    }
    if (appid == "") {
        mviewToastr("error","请选择小程序APPID!");
        return;
    }
    if (comment == "") {
        mviewToastr("error","请输入小程序简介!");
        return;
    }
    if (comment.length < 20 || comment.length > 200) {
        mviewToastr("error","小程序经营内容限制20-200字!");
        return;
    }
    if ($("#admin_shouquan").val() == 2 && shqshUrl=="") {
        mviewToastr("error","请上传小程序授权书!");
        return;
    }
    mviewToastr('load','正在提交备案,请耐心等待...',20,false);
    $.ajax({
        "url": "/admin/miniapp/miniapp_beian_submit",
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        "type": "POST",
        "data": {"corp_id":corp_id,"admin_id":admin_id,"appid":appid,"comment":comment,"shqshUrl":shqshUrl,"verify_task":$("input[name='VerifyTask']").val(),"applets_other_materials_url":applets_other_materials_url},
        success: function(res){
            if (res.status == "success") {
                colsemviewToastr();
                mviewToastr("success",res.msg);
                setTimeout(()=>{
                    window.location.replace('/admin/miniapps/miniapp_detial/'+appid);
                },1000);
            } else {
                let err_text = '';
                $.each(res.data, function(index, val) {
                     err_text = val.errmsg + ";" + err_text;
                });
                mviewToastr("error",err_text,5);
            }
        },
        error: function(err){
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}

function createIcpVerifyTask() {
    var appid = $("#appid").val();
    if (appid == "") {
        mviewToastr("error","请先选择小程序APPID!");
        return;
    }
    $.ajax({
        "url": "/admin/miniapp/miniapp_beian_verify",
        "type": "GET",
        "data": {"appid":appid},
        success: function(res){
            console.log(res);
            if(res.status == 'success'){
                if(res.msg.errcode == 0){
                    if(res.msg.is_finish && res.msg.face_status == 3){
                        //人脸成功
                        $("input[name='VerifyTask']").val(res.msg.task_id);
                        $('.beian_1').hide();
                        //选择模板
                        $('.beian_2').show();
                        if($("#admin_shouquan").val() == 2){
                            $("#shouquan-show").show();
                        }else{
                            $("#shouquan-show").hide();
                        }
                    }else{
                        $("#verify-task").text('人脸通过后点击这里验证');
                    }
                }else{
                    mviewToastr("error",res.msg);
                }
            }else{
                mviewToastr("error",res.msg);
            }
        },
        error: function(err){
            console.log();
            mviewToastr("error","网络异常,请刷新网页后重试!");
        }
    });
}